package com.infore.siteManagement.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.constant.ResultConstants;
import com.infore.siteManagement.entity.StationType;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IStationTypeRespository;
import com.infore.siteManagement.select.QueryDispatcher;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.QueryUtil;
import com.infore.siteManagement.service.IStationTypeService;
import com.infore.siteManagement.util.DateUtil;
import com.infore.siteManagement.util.MyBeanUtils;
import com.infore.siteManagement.util.Page;
import com.infore.siteManagement.util.PageUtil;
import com.infore.siteManagement.util.ResultEntity;
/**
 * 站点类型管理服务实现类
 * @author 刘乐法
 *
 *
 */
@Service
public class IStationTypeServiceImpl implements IStationTypeService{

	@Autowired
	private IStationTypeRespository iStationTypeRespository;
	@Autowired
	private QueryDispatcher queryDispatcher;
	
	public ResultEntity queryStationType(QueryParamDto queryParamDto) {
		if (StringUtils.isEmpty(queryParamDto)) {//无任何条件查询
			List<StationType> list = iStationTypeRespository.findAll();
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		//仅分页查询
		}else if (!StringUtils.isEmpty(queryParamDto.getPageObject()) && 
				StringUtils.isEmpty(queryParamDto.getQueryCondition()) &&
				StringUtils.isEmpty(queryParamDto.getAssembleCondition()) && 
				StringUtils.isEmpty(queryParamDto.getAssembleSql())) {
			org.springframework.data.domain.Page<StationType> pageable = iStationTypeRespository.findAll(queryParamDto.getPageObject().getPageRequest());
			Page<StationType> page = PageUtil.pageConvert(pageable);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		//只传属性不传字段的查询
		}else if (org.apache.commons.lang.StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) {
			return this.queryAll(queryParamDto);
		}
		//指定字段查询
		return queryDispatcher.dispatcher(queryParamDto, StationType.class);
	}
	
	@SuppressWarnings("serial")
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		//判断是否有条件再判断是否需要分页,不分页必须传null
		if (!StringUtils.isEmpty(queryParamDto.getPageObject())) {
			//需要分页
			org.springframework.data.domain.Page<StationType> pageAble =  iStationTypeRespository.findAll(new Specification<StationType>() {
				public Predicate toPredicate(Root<StationType> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			}, queryParamDto.getPageObject().getPageRequest());
			Page<StationType> page = PageUtil.pageConvert(pageAble);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		}else {
			List<StationType> list = iStationTypeRespository.findAll(new Specification<StationType>() {
				public Predicate toPredicate(Root<StationType> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			});
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		}
	}

	public ResultEntity deleteStationType(String ids) {
		if (ids.contains(",")) {
			List<String> idsList = Arrays.asList(ids.split(","));
			iStationTypeRespository.deleteByStationTypeIdIn(idsList);
		} else {
			iStationTypeRespository.deleteById(ids);
		}
		return new ResultEntity(ResultStatus.SUCCESS,Constants.DELETE_SUCCESS);
	}

	public ResultEntity editStationType(StationType stationtype) {
		Date time = DateUtil.getSystemTime();
		stationtype.setLastUpdateTime(time);
		//如果为空则新增 不为空则编辑
		//flag用来判断返回Constants.UPDATE_SUCCESS还是Constants.INSERT_SUCCESS
		Boolean flagBoolean = false;//编辑true新增false
		//如果id不为空则查询数据库中的对象 进行编辑
		if (!StringUtils.isEmpty(stationtype.getStationTypeId())) {
			StationType oldStationtype = iStationTypeRespository.findById(stationtype.getStationTypeId()).orElse(null);
			//如果数据库中对象存在
			//防止编辑时传入对象中的null值覆盖原有数据库中的非空值
			if (!StringUtils.isEmpty(oldStationtype)) {
				MyBeanUtils.copyNonNullProperties(stationtype, oldStationtype);
				stationtype = oldStationtype;
			} else {
				return new ResultEntity(ResultStatus.ERROR,ResultConstants.NO_SUCH_OBJECT);
			}
			flagBoolean = true;
		}
		//验证站点类型
		ResultEntity resultEntity = validateStationType(stationtype);
		//如果验证失败
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		iStationTypeRespository.saveAndFlush(stationtype);
		if (flagBoolean) {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.UPDATE_SUCCESS);
		}else {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.INSERT_SUCCESS);
		}
		
	}

	public ResultEntity validateStationType(StationType stationtype) {
		String message = "验证站点类型成功";
		if (stationtype.getStationTypeName().length() > 50) {
			message = "站点类型名称过长";
		}else if (stationtype.getRemark().length() > 50) {
			message = "备注过长";
		}else if (stationtype.getStationTypeCode().length() > 20) {
			message = "站点类型编码过长";
		}
		if (("验证站点类型成功").equals(message)) {
			return new ResultEntity(ResultStatus.SUCCESS,message);
		}else {
			return new ResultEntity(ResultStatus.ERROR,message);
		}
	}

}
